<template>
<section>
  <el-row>
    <el-col :span="6">
      <el-input placeholder="请输入内容" v-model="searchKey" prefix-icon="el-icon-fa-search">
        <div slot="append">
          <el-button type="primary" icon="el-icon-fa-search" @click=" pager.pager.pageNumber = 1 ;doSearch()"></el-button>
        </div>
      </el-input>
    </el-col>
    <el-col :span="6" :offset="12">
      <el-button type="primary" icon="el-icon-fa-plus" @click="addTransport" size="small">添加进出口信息</el-button>
    </el-col>
  </el-row>
  <el-table :data="pager.dataList" border stripe style="width: 100%">
    <el-table-column prop="id" label="ID" header-align="center" align="center" width="55">
    </el-table-column>
    <el-table-column prop="poe" label="起运港" show-overflow-tooltip header-align="center" align="center">
    </el-table-column>
    <el-table-column prop="shippingTime" label="起运时间" show-overflow-tooltip header-align="center" align="center">
    </el-table-column>
    <el-table-column prop="poi" label="目的港" show-overflow-tooltip header-align="center" align="center">
    </el-table-column>
    <el-table-column prop="arrivalTime" label="抵达时间" show-overflow-tooltip header-align="center" align="center">
    </el-table-column>
    <el-table-column label="操作" show-overflow-tooltip header-align="center" align="center">
      <template slot-scope="scope">
          <el-button-group>
            <el-button title="编辑进出口信息" size="mini" type="primary" icon="el-icon-fa-edit" @click="handleEdit(scope.$index,scope.row)"></el-button>
            <el-button title="删除" size="mini" type="primary" icon="el-icon-fa-trash" @click="handleDelete(scope.$index,scope.row)"></el-button>
          </el-button-group>
      </template>
    </el-table-column>
  </el-table>
  <!--分页条 -->
  <el-row>
    <el-col :span="6" :offset="18">
      <el-pagination background small style="float:right" layout="prev, pager, next" :total="pager.pager.recordCount" :page-size="pager.pager.pageSize" :current-page.sync="pager.pager.pageNumber" v-show="pager.pager.pageCount != 0" @current-change="changePage">
      </el-pagination>
    </el-col>
  </el-row>
  <!-- 弹框区域-->
  <el-dialog :title="transport.id == 0 ? '添加进出口信息' : '编辑进出口信息' " :visible.sync="addEditShow" width="75%">
    <el-row>
      <el-col :span="12">
        <el-form :model="transport" :rules="$rules.transport" ref="transportForm">
          <el-form-item label="起运港" :label-width="formLabelWidth" prop="poe">
            <el-input v-model="transport.poe" auto-complete="off" suffix-icon="el-icon-fa-vcard" placeholder="请填写起运港"></el-input>
          </el-form-item>
          <el-form-item label="起运时间" :label-width="formLabelWidth" prop="shippingTime">
            <el-date-picker value-format="yyyy-MM-dd" v-model="transport.shippingTime" type="date" placeholder="选择起运时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="商检编号" :label-width="formLabelWidth" prop="commodityInspectionNo">
            <el-input v-model="transport.commodityInspectionNo" auto-complete="off" suffix-icon="el-icon-fa-vcard" placeholder="请填写商检编号"></el-input>
          </el-form-item>

          <el-form-item label="提单" :label-width="formLabelWidth" prop="name">
            <el-upload class="avatar-uploader" :action="imgupload" name="imgFile" :data="{key:'loadingBillImgKey'}" :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
              <img v-if="transport.loadingBillImgKey" :src="transport.loadingBillImgKey" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="原产地证" :label-width="formLabelWidth" prop="cooImgKey">
            <el-upload class="avatar-uploader" :action="imgupload" name="imgFile" :data="{key:'cooImgKey'}" :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
              <img v-if="transport.cooImgKey" :src="transport.cooImgKey" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>

          <el-form-item label="ICO证书" :label-width="formLabelWidth" prop="icoImgKey">
            <el-upload class="avatar-uploader" :action="imgupload" name="imgFile" :data="{key:'icoImgKey'}" :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
              <img v-if="transport.icoImgKey" :src="transport.icoImgKey" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="商检局卫生证" :label-width="formLabelWidth" prop="hcbciImgKey">
            <el-upload class="avatar-uploader" :action="imgupload" name="imgFile" :data="{key:'hcbciImgKey'}" :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
              <img v-if="transport.hcbciImgKey" :src="transport.hcbciImgKey" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
            <el-upload v-show="false" class="avatar-uploader" :action="imgupload" name="imgFile" :data="{key:'hcbciImgKey'}" :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
              <img v-if="imageUrl" :src="imageUrl" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
        </el-form>
      </el-col>
      <el-col :span="12">
        <el-form :model="transport" :rules="$rules.transport" ref="transportForm1">
          <el-form-item label="目的港" :label-width="formLabelWidth" prop="poi">
            <el-input v-model="transport.poi" auto-complete="off" suffix-icon="el-icon-fa-vcard" placeholder="请填写起运港"></el-input>
          </el-form-item>
          <el-form-item label="抵达时间" :label-width="formLabelWidth" prop="arrivalTime">
            <el-date-picker value-format="yyyy-MM-dd" v-model="transport.arrivalTime" type="date" placeholder="选择抵达时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="商检结果" :label-width="formLabelWidth" prop="commodityInspectionResult">
            <el-input v-model="transport.commodityInspectionResult" auto-complete="off" suffix-icon="el-icon-fa-vcard" placeholder="请填写商检结果"></el-input>
          </el-form-item>

          <el-form-item label="报关单" :label-width="formLabelWidth" prop="customsDeclarationImgKey">
            <el-upload class="avatar-uploader" :action="imgupload" name="imgFile" :data="{key:'customsDeclarationImgKey'}" :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
              <img v-if="transport.customsDeclarationImgKey" :src="transport.customsDeclarationImgKey" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="植物检验检疫证书" :label-width="formLabelWidth" prop="piqcImgKey">
            <el-upload class="avatar-uploader" :action="imgupload" name="imgFile" :data="{key:'piqcImgKey'}" :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
              <img v-if="transport.piqcImgKey" :src="transport.piqcImgKey" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="品质证书" :label-width="formLabelWidth" prop="qcImgKey">
            <el-upload class="avatar-uploader" :action="imgupload" name="imgFile" :data="{key:'qcImgKey'}" :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
              <img v-if="transport.qcImgKey" :src="transport.qcImgKey" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="商检结果证书" :label-width="formLabelWidth" prop="commodityInspectionResultImgKey">
            <el-upload class="avatar-uploader" :action="imgupload" name="imgFile" :data="{key:'commodityInspectionResultImgKey'}" :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
              <img v-if="transport.commodityInspectionResultImgKey" :src="transport.commodityInspectionResultImgKey" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>

    <div slot="footer" class="dialog-footer">
      <el-button @click="addEditShow = false ; transport = {id:0}">取 消</el-button>
      <el-button type="primary" @click="saveOrUpdateTransport('transportForm')">确 定</el-button>
    </div>
  </el-dialog>
</section>
</template>

<style>
.el-date-editor {
  width: 100% !important;
}
</style>

<script>
import config from "../../common/config";
export default {
  data() {
    return {
      imgupload: config.http.baseUrl + "image/kind/upload",
      searchKey: "",
      pager: {
        pager: {
          pageCount: 1,
          pageNumber: 1,
          pageSize: 15,
          recordCount: 1
        },
        dataList: []
      },
      addEditShow: false,
      imageUrl: "",
      formLabelWidth: "140px",
      transport: {
        id: 0
      },
      areas: []
    };
  },
  methods: {
    loadData() {
      this.$api.Transport.list(this.pager.pager.pageNumber, result => {
        this.pager = result.pager;
      });
    },
    addTransport() {
      this.addEditShow = true;
      this.transport = {
        id: 0
      };
    },
    handleEdit(index, row) {
      this.transport = row;
      this.addEditShow = true;
    },
    handleDelete(index, row) {
      this.$confirm("确认删除船运信息?", "删除确认", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.$api.Transport.delete(row.id, result => {
          this.$message({
            type: "success",
            message: "删除成功!"
          });
          window.setTimeout(() => {
            if (this.searchKey) {
              this.doSearch();
            } else {
              this.loadData();
            }
          }, 2000);
        });
      });
    },
    changePage() {
      if (this.searchKey) {
        this.doSearch();
      } else {
        this.loadData();
      }
    },
    doSearch() {
      this.$api.transport.search(
        this.pager.pager.pageNumber,
        this.searchKey,
        result => {
          this.pager = result.pager;
        }
      );
    },
    saveOrUpdateTransport(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          var callback = result => {
            this.changePage();
            this.addEditShow = false;
          };
          this.transport.id
            ? this.$api.Transport.update(this.transport, callback)
            : this.$api.Transport.save(this.transport, callback);
        } else {
          return false;
        }
      });
    },
    handleAvatarSuccess(res, file) {
      this.transport[res.key] = config.http.baseUrl + res.url;
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isIMG = file.type.substr(0, "image/".length) === "image/";
      console.log(isIMG);
      const isLt5M = file.size / 1024 / 1024 < 5;
      if (!isIMG) {
        this.$message.error("只能上传图片格式!");
      }
      if (!isLt5M) {
        this.$message.error("上传图片大小不能超过 5 MB!");
      }
      return isIMG && isLt5M;
    }
  },
  created() {
    this.loadData();
  }
};
</script>
